﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BO;
using UIControls;
using System.Data.SqlClient;
using BrightIdeasSoftware;
using System.Windows.Forms.Design;
using System.Diagnostics;
using System.Collections;
using System.Data.Entity;
using UI.Core;
namespace UI
{
    public partial class DateTargetEditForm : EditFormBase, IMenuForm
    {
        public DateTargetEditForm()
        {
            InitializeComponent();
        
            MyGrid = grid;

            dateBegin.DateTimePicker.CustomFormat = "yyyy-MM";
            dateEnd.DateTimePicker.CustomFormat = "yyyy-MM";
        }

        protected void FildChanged(object sender, EventArgs e)
        {
            base.FildChanged( sender,  e);
        }
        DateTargetsList mySource;
        protected DateTime BeginDate
        {
            get {
                var d = dateBegin.DateTimePicker.Value.Date;
                return new DateTime(d.Year,d.Month,1); }
        }
        protected DateTime EndDate
        {
            get  { var d = dateEnd.DateTimePicker.Value.Date;
                return new DateTime(d.Year,d.Month,1); }
        }

      
        protected string centerName
        {
            get
            {
                if (cbCenter.ComboBox.SelectedValue == null)
                    return string.Empty;
                return cbCenter.ComboBox.SelectedValue.ToString();
            }
        }
        protected string companyName
        {
            get
            {
                if (cbCompany.ComboBox.SelectedValue == null)
                    return string.Empty;
                return cbCompany.ComboBox.SelectedValue.ToString();
            }
        }

        protected string departmentName
        {
            get
            {
                if (cbDepartment.ComboBox.SelectedValue == null)
                    return string.Empty;
                return cbDepartment.ComboBox.SelectedValue.ToString();
            }
        }


        protected override void OnGridFilterd(UIControls.Grid.OSGrid.FilterdEventArgs e)
        {
            double t = 0;
            long cnt = 0;
            foreach (DateTargets item in e.FilteredObjects)
            {
                t += item.Amount;
                cnt++;
            }
            TotalAmt = t;
            RowCount = cnt;
        }

        void BindFileds()
        {
            var d1 = ListFactory<CompanyList>.GetList("");
            d1.Insert(0, new Company() { Name = "", });
            cbCompany.ComboBox.DataSource = d1;
            cbCompany.ComboBox.ValueMember = "Name";
            cbCompany.ComboBox.DisplayMember = "Name";

            var d2 = ListFactory<DepartmentList>.GetList("");
            d2.Insert(0, new Department() { Name = "", });
            cbDepartment.ComboBox.DataSource = d2;
            cbDepartment.ComboBox.ValueMember = "Name";
            cbDepartment.ComboBox.DisplayMember = "Name";

       
            var d5 = ListFactory<CenterList>.GetList("");
            d5.Insert(0, new Center() { Name = "", });
            cbCenter.ComboBox.DataSource = d5;
            cbCenter.ComboBox.ValueMember = "Name";
            cbCenter.ComboBox.DisplayMember = "Name";
        }

        private void Form_Load(object sender, EventArgs e)
        {
            BindFileds();
            var d = DateTime.Now;
            dateBegin.DateTimePicker.Value = new DateTime(d.Year, 1, 1);
            dateEnd.DateTimePicker.Value = d;
           
            OnSetGridColumns();
            InitGrid();
            OnQuery();
            MyGrid.RowObjectType = typeof(DateTargets);
        }
        double TotalAmount;
        long rowCount;
        protected override void OnQuery()
        {
            BindFileds();
            base.OnQuery();
        }
        protected override void FetchData()
        {
            mySource = ListFactory<DateTargetsList>.GetList("");
                //(from item in mySource.Local
                //        where
                //        item.CompanyName.Contains(companyName)
                //        && item.CenterName.Contains(centerName)
                //        && item.DepartmentName.Contains(departmentName)
                //        orderby item.BeginDate descending, item.EndDate descending
                //        select item
                //           ).ToList();
            
            GridDataSource = mySource;
            rowCount = mySource.Count;
            TotalAmount = mySource.Sum(item => item.Amount);
        }
        protected override void DataBind()
        {
            base.DataBind();
            TotalAmt = mySource.Sum(item => item.Amount);
            RowCount = MyGrid.Items.Count;
        }
        void OnSetGridColumns()
        {
            var cols = new List<OLVColumn>();

            cols.Add(new OLVColumn("编号", "ID") { IsEditable = false, Width = 50 });
            var date1 = new OLVColumn("开始日期", "BeginDate")
            {
                Width = 110,
                AspectToStringFormat = "{0:d}",
                ClusteringStrategy = new DateTimeClusteringStrategy(DateTimePortion.Year | DateTimePortion.Month | DateTimePortion.Day, "{0:d}"),
            };
            cols.Add(date1);
            var date2= new OLVColumn("结束日期", "EndDate")
            {
                Width = 110,
                AspectToStringFormat = "{0:d}",
                ClusteringStrategy = new DateTimeClusteringStrategy(DateTimePortion.Year | DateTimePortion.Month | DateTimePortion.Day, "{0:d}"),
            };
            cols.Add(date2);
            cols.Add(new OLVColumn(FieldHelper.cnName.Company, "CompanyName") { Width = 60 });
            cols.Add(new OLVColumn(FieldHelper.cnName.Center, "CenterName") { Width = 110 });
            cols.Add(new OLVColumn(FieldHelper.cnName.Department, "DepartmentName") { Width = 110 });
          
            var cAmount = new OLVColumn("业绩目标", "Amount");
            
            cAmount.Width = 80;
            cols.Add(cAmount);
        

            MyGrid.AllColumns.AddRange(cols);


            MyGrid.Columns.AddRange(cols.ToArray());


        }
       

    
        protected override object OnAdd()
        {
            var obj = new DateTargets();
            obj.CompanyName = companyName;
            obj.DepartmentName = departmentName;
            obj.CenterName = centerName;

            obj.BeginDate =
                obj.EndDate = DateTime.Now;

            return obj;
        }



        protected override void OnSave()
        {
            try
            {
                foreach (DateTargets delObj in RemovedList)
                {
                    if (delObj == null)
                        continue;
                    if (delObj.ID == 0)
                        continue;
                    mySource.Remove(delObj);
                }
                RemovedList.Clear();

                foreach (DateTargets obj in MyGrid.Objects)
                {
                    if (obj == null)
                        continue;
                    if (obj.ID == 0)
                        mySource.Add(obj);

                }

                mySource.Save();
                OnQuery();
            }
            catch (ExceptionEx exp)
            {
                ExcptionHandler.ShowExcption(exp);
            }

        }


     
    }
}
